import { defineStore } from 'pinia'
import { GetMusic } from '@/models/music'
import { musicStore } from './musicStore'
import { getAllLyric, getMusicUrl } from '@/utils/api'
import { getMusic } from './getMusic'
import { GetAllDetailMusics } from '@/models/musciLists'
import { ref } from 'vue'

const musicStore2 = musicStore()
const music2 = getMusic()

const key = {
    'volume': 'volume'
}

export const musicPlay = defineStore("musicPlay", {
    state: () => {
        return {
            audio: new Audio,
            audioDuration: 0,/* 音乐对象时间总长 */
            audioCurrent: 0,/* 音频当前播放时间 */
            audioVolume: Number(localStorage.getItem(key.volume)) || 60,/* 音频声音 */
            audioPlay: false,/* 音频播放状态 */
            audioName: '暂无音乐',/* 音乐名字 */
            audioLove: 0,
            playProgress: 0,/* 音频播放进度 */
            url: '',/* 音乐路径 */
            id: -1,/* 音乐序号 */
            img: 'https://img.aidotu.com/down/jpg/20200827/52158ceea6f2630d08867b0899aa6bc9_18497_240_240.jpg',/* 音乐相册 */
            index: -1,/* 记录音乐播放第几个 */
            music: {} as GetMusic, /* 存音乐的相关信息 */
            musicInfo: {} as GetAllDetailMusics,/* 存放音乐全部信息 */
            laterMusic: [] as GetAllDetailMusics[] /* 存播放列表的音乐 */
        }
    },
    actions: {
        /* 开始播放音乐 */
        playMusic(lists: GetMusic, index: number) {
            // console.log(lists)
            // console.log(this.audio.currentTime)
            // console.log(lists)
            this.audio.src = lists.url
            this.audio.volume = this.audioVolume / 100
            this.audio.play().then(() => {
                // console.log('播放成功')
                this.id = lists.id
                this.img = this.musicInfo.al.picUrl
                this.url = lists.url
                this.audioPlay = true
                this.audioName = this.musicInfo.name
                this.audioDuration = this.musicInfo.dt
                this.audioCurrent = this.audio.currentTime
                this.index = index
                this.updateProgress()
            }).catch(res => {
                console.log(res)
            })
        },
        /* 暂停和开始 */
        StopOrPlay() {
            // console.log(this.url)
            if (this.url == '') {
                return null
            }
            if (this.audioPlay) {
                this.audioPlay = !this.audioPlay
                this.audio.pause()
                return true
            } else {
                this.audioPlay = !this.audioPlay
                this.audio.play()
                return true
            }
        },
        /* 查看是否是同一个音乐地址和id */
        SameOrDif(lists: GetAllDetailMusics, index: number) {
            musicStore2.pushLaterListenMusics(lists)
            if (lists.id == this.id) {
                this.StopOrPlay()
            } else {
                this.getMusicUrl(lists.id, index)
            }
        },
        /* 进行上一首播放或者下一首播放 */
        NextOrPrev(num: number) {
            // console.log('@@', this.index)
            // console.log('第几个', musicStore2.tryListenMusics.length)
            if (this.laterMusic.length == 0) {
                return '暂时无歌曲'
            }
            if (num < 0 && this.index == 0) {
                return '已经是第一首了'
            } else if ((this.index + num) == this.laterMusic.length) {
                return '已经是最后一首了'
            } else {
                this.nextMusicUrl(this.laterMusic[this.index + num].id, this.index + num)
                return true
            }
        },
        /* 上一首音乐和下一首音乐特别的 */
        async nextMusicUrl(id: number, index: number) {
            let { code, data } = await getMusicUrl(id)
            if (code == 200) {
                if (data[0].code == 404) {
                    this.NextOrPrev(1)
                }
                this.music = data[0]
                /* 获取到全部音乐的详细信息 */
                this.musicInfo = this.laterMusic[index]
                /* 播放音乐 */
                this.playMusic(this.music, index)
                /* 放入最近播放中 */
                musicStore2.pushLaterListenMusics(this.musicInfo)
            } else {
                return false
            }
        },
        /* 获取到音乐地址 */
        async getMusicUrl(id: number, index: number) {
            let { code, data } = await getMusicUrl(id)
            if (code == 200) {
                if (data[0].code == 404) {
                    this.NextOrPrev(1)
                }
                this.music = data[0]
                /* 存放在播放列表 */
                this.saveLaterMusic()
                /* 获取到全部音乐的详细信息 */
                this.musicInfo = this.laterMusic[index]
                console.log(this.musicInfo)
                /* 播放音乐 */
                this.playMusic(this.music, index)
                /* 放入最近播放中 */
                musicStore2.pushLaterListenMusics(this.musicInfo)
            } else {
                return false
            }
        },
        /* 获取到音乐进度条进行中 */
        updateProgress() {
            this.audio.addEventListener("timeupdate", () => {
                this.audioCurrent = this.audio.currentTime;
            });
        },
        /* 保存播放列表 */
        saveLaterMusic() {
            this.laterMusic = music2.allMusics
        },
        /* 音量存储 */
        updateVolume(n: number) {
            n > 100 ? 100 : n
            n < 0 ? 0 : n
            this.audioVolume = n
            this.audio.volume = n / 100
            localStorage.setItem(key.volume, n.toString())
        },
        /* 进度条更新 */
        playingProgress(left: number) {
            let b = left / 99 * (this.audioDuration / 1000)
            this.audioCurrent = b
        },
        playingProgress2(left: number) {
            let b = left / 99 * (this.audioDuration / 1000)
            this.audio.currentTime = b
        },

    },
    getters: {

    }
})

